package com.mimosa.common.system.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class ProductUserContext implements UserContext {
	/**
	 * 单一实例
	 */
	private static TUser SINGLE_INSTANCE = new TUser();

	@Override
	public String currentUserName() {
		return this.currentUser().getUserName();
	}

	@Override
	public String currentRealName() {
		return this.currentUser().getRealName();
	}

	@Override
	public String currentUserId() {
		return this.currentUser().getId();
	}

	@Override
	public TUser currentUser() {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		TUser user = SINGLE_INSTANCE;
		if (servletRequestAttributes != null) {
			HttpServletRequest request = servletRequestAttributes.getRequest();
			HttpSession httpSession = request.getSession();
			user = (TUser) httpSession.getAttribute(UserContext.USER_ATTR);
		}
		if (user == null) {
			user = SINGLE_INSTANCE;
		}
		return user;
	}

	@Override
	public Integer currentUserSecretLevel() {
		return this.currentUser().getSecretLevel();
	}

	public String currentCustomerId() {
		return null;
	}
}